/*
* @Author: Adolphus.Yao
* @Date:   2019-07-23 17:17:49
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2020-02-05 18:08:51
*/
package main

import (
    "flag"
    "bufio"
    "fmt"
    "io"
    "os"
    "strings"
    "encoding/json"
)

const (
    CARD_STATUS_VALID   = 0
    CARD_STATUS_INVALID = 1
)

var (
    h bool
    file_path string
    cdt map[string]map[string]int
)

func init() {
    flag.BoolVar(&h, "h", false, "this help")
    flag.StringVar(&file_path, "f", "out.txt", "set json datas file")

    // 改变默认的 Usage，flag包中的Usage 其实是一个函数类型。这里是覆盖默认函数实现，具体见后面Usage部分的分析
    flag.Usage = usage

    cdt = make(map[string]map[string]int)
}

func main() {
    flag.Parse()

    if h {
        flag.Usage()
    } else {
        fi, err := os.Open(file_path)
        if err != nil {
            fmt.Printf("Error: %s\n", err)
            return
        }
        defer fi.Close()

        br := bufio.NewReader(fi)
        lineNo := 0
        for {
            lineNo++
            line, err := br.ReadString('\n')
            line = strings.TrimSpace(line)
            if err != nil {
                if io.EOF == err {   //读取结束，会报EOF
                    break
                }
                fmt.Println(err, " err!!!!!!!!!! ", lineNo)
                continue
            }
            xline := strings.Replace(line, "[32;1minfo[0m", "info", 1)
            checkJson(xline)
        }
        jsonStr, _ := json.Marshal(cdt)
        fmt.Println(string(jsonStr))
    }
}

func usage() {
    fmt.Fprintf(os.Stderr, `jcheck version: jcheck/1.0.0
Usage: jcheck

Options:
`)
    flag.PrintDefaults()
}

func checkJson(jsonStr string) {
    var mapResult map[string]interface{}
    err := json.Unmarshal([]byte(jsonStr), &mapResult)
    if nil != err {
        fmt.Println("JsonToMapDemo err: ", err, " json:", jsonStr)
    }
    if _v0, ok0 := mapResult["fullReqPath"]; ok0 {
        v0 := fmt.Sprintf("%s", _v0)
        if _v1, ok1 := mapResult["errCode"]; ok1 {
            v1 := fmt.Sprintf("%s", _v1)
            if "0" == v1 {
                return
            }
            if v00, ok00 := cdt[v0]; ok00 {
                if v11, ok11 := v00[v1]; ok11 {
                    v11 = v11 + 1
                    v00[v1] = v11
                } else {
                    v00[v1] = 1
                }
            } else {
                v00 := make(map[string]int)
                v00[v1] = 1
                cdt[v0] = v00
            }
        }
    }
}
/*
如上逻辑提取数据分析，仅仅是在10月19日后的数据是否存在重复和脱敏的，对于之前重复和脱敏的，没提取，是因为这类数据是等待自动修复的，目前发现是c的公积金和出生证还是有问题
公积金是存在脱敏，出生证、公积金、社保、驾驶证，都是是存在多个重复，重复数据是脱敏的
 */